package com.sebiotec.basecommon.utils.tools;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;


import com.jakewharton.rxbinding2.view.RxView;
import com.sebiotec.basecommon.R;
import com.sebiotec.basecommon.utils.constant.SjmConstant;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;

public class LangDialogFragment extends DialogFragment {


    private TextView tvTitle;
    private TextView tvYes;
    private TextView tvNo;
    private String tittle;

    public interface ConfirmListener {
        void onClickComplete(String tittle);
    }


    public static LangDialogFragment newInstance(String tittle) {
        LangDialogFragment fragment = new LangDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tittle", tittle);

        fragment.setArguments(bundle);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);


    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
         tittle = getArguments().getString("tittle");
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         View view= LayoutInflater.from(getActivity()).inflate(R.layout.dialog_lanughs, null);
          tvTitle=(TextView)view.findViewById(R.id.tv_title);
           if ("1".equals(tittle)){
               tvTitle.setText(getString(R.string.tokened));
           }else{
               tvTitle.setText(getString(R.string.re_logins));
           }

          tvYes=(TextView)view.findViewById(R.id.tv_yes);
          tvNo=(TextView)view.findViewById(R.id.tv_no);
          initListener();

        builder.setView(view);
        return builder.create();

    }

    private void initListener() {

        RxView.clicks(tvYes)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        Log.d("重复点击","1");
//                        ConfirmListener confirmListener = (ConfirmListener) getActivity();
//                        confirmListener.onClickComplete(tittle);
//                        dismiss();

                        SPUtils.remove(SjmConstant.TOKEN);
                        ActivityTool.getInstance().finishAllActivities();

                    }
                });

        RxView.clicks(tvNo)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
//                        dismiss();

                        SPUtils.remove(SjmConstant.TOKEN);
                        ActivityTool.getInstance().finishAllActivities();
//                        ARouter.getInstance().build("/login/LoginActivity").navigation();
                    }
                });


    }

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);

        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        DisplayMetrics dm = new DisplayMetrics();

        getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.gravity= Gravity.CENTER;

        windowParams.width = (new Double(dm.widthPixels*0.7)).intValue(); //屏幕宽度
        windowParams.height = dm.heightPixels/3; //屏幕高度的1/3
        window.setAttributes(windowParams);


    }
}
